package com.tyut.homework.repository;

import com.tyut.homework.dto.ClazzPeopleNum;
import com.tyut.homework.dto.DorClazz;
import com.tyut.homework.pojo.Dormitory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Repository
public interface DormitoryRepository extends JpaRepository<Dormitory,Integer> {

    @Query(value = "select  clazz,count(sex)/4+1 as count from student where sex='男' group by clazz",nativeQuery = true)
    List<ClazzPeopleNum> selectPeopleNumMan();

    @Query(value = "select d from Dormitory d where d.status=0")
    List<Dormitory> selectAllNotMatch();

    @Transactional
    @Modifying
    @Query(value = "update dormitory set status=1 where id=?1",nativeQuery = true)
    int updateStatus(Integer id);

    @Transactional
    @Modifying
    @Query(value = "update dormitory set status=1,clazz=?1 where id=?2",nativeQuery = true)
    void updateClazz(@Param("clazz") String clazz,@Param("id") Integer id);

    List<Dormitory> findAllByClazz(String clazz);

//    @Query("select  new com.tyut.homework.dto.ClazzPeopleNum(clazz,count(sex)) from student where sex='女' group by clazz")
//    List<ClazzPeopleNum> selectPeopleNumWoMan();
}
